package com.cjjd.c.feature.mine.information;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cjjd.c.R;
import com.cjjd.c.ShareKeys;
import com.cjjd.c.base.ActionBarFragment;
import com.cjjd.c.bean.HttpResult;
import com.cjjd.c.bean.ReceiptInfoBean;
import com.cjjd.c.http.CallbackImpl;
import com.cjjd.c.http.JsonUtils;
import com.cjjd.c.utils.SharePrefUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * ----------------------------------
 * Created by xyd on 2017/8/18.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class ReceiptInfoFragment extends ActionBarFragment {


    @Bind(R.id.tv_type)
    TextView tvType;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_num)
    TextView tvNum;
    @Bind(R.id.tv_address)
    TextView tvAddress;
    @Bind(R.id.tv_mobile)
    EditText tvMobile;
    @Bind(R.id.tv_bank_name)
    TextView tvBankName;
    @Bind(R.id.tv_bank_num)
    TextView tvBankNum;
    @Bind(R.id.tv_phone)
    TextView tvPhone;
    @Bind(R.id.tv_email_address)
    TextView tvEmailAddress;
    @Bind(R.id.tv_receipt_del)
    TextView tvReceiptDel;
    @Bind(R.id.ll_compny_info)
    LinearLayout ll_compny_info;

    @Override
    public Object getTitleString() {
        return "发票详情";
    }

    @Override
    public int getContentView() {
        return R.layout.fragment_receipt_info;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        initView();
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    protected void initView() {

        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
        h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
        h1.put("uuid", uuid);


        Call<HttpResult<ReceiptInfoBean>> call = service.getMemberInvoiceDetail(JsonUtils.convertParam("getMemberInvoiceDetail", h1));
        call.enqueue(new CallbackImpl<ReceiptInfoBean>() {
            @Override
            public void responseSuccess(Call<HttpResult<ReceiptInfoBean>> call, Response<HttpResult<ReceiptInfoBean>> response) {
                ReceiptInfoBean data = response.body().getData();
                if (data != null) {
                    ReceiptInfoBean.MemberInvoiceModelBean memberInvoiceModel = data.memberInvoiceModel;
                    if (memberInvoiceModel != null) {

                        if ("1".equals(memberInvoiceModel.invoiceType)) {
                            // 个人
                            ll_compny_info.setVisibility(View.GONE);
                            tvNum.setVisibility(View.GONE);
                            tvType.setText("个人");
                        } else if ("2".equals(memberInvoiceModel.invoiceType)) {
                            // 单位
                            ll_compny_info.setVisibility(View.VISIBLE);
                            tvNum.setVisibility(View.VISIBLE);

                            tvType.setText("单位");
                            tvAddress.setText(memberInvoiceModel.address);
                            tvMobile.setText(memberInvoiceModel.telephone);
                            tvBankName.setText(memberInvoiceModel.bankName);
                            tvBankNum.setText(memberInvoiceModel.bankCard);
                            tvNum.setText(memberInvoiceModel.dutyParagraph);
                        }

                        tvName.setText(memberInvoiceModel.company);
                        tvPhone.setText(memberInvoiceModel.mobile);
                        tvEmailAddress.setText(memberInvoiceModel.postAddress);

                    }

                }
            }
        });
    }

    @Override
    public void initRightButton(Button rightButton) {
        super.initRightButton(rightButton);
        rightButton.setText("保存");
        rightButton.setVisibility(View.VISIBLE);
    }


    @OnClick({R.id.btn_right, R.id.tv_receipt_del})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.btn_right:
                mContext.onBackPressed();
                break;

            case R.id.tv_receipt_del:
                delMemberInvoice();
                break;

        }
    }

    private void delMemberInvoice() {
        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
        h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
        h1.put("uuids", uuid);


        Call call = service.delMemberInvoice(JsonUtils.convertParam("delMemberInvoice", h1));
        call.enqueue(new CallbackImpl() {
            @Override
            public void responseSuccess(Call call, Response response) {
                mContext.onBackPressed();
            }
        });
    }

    private String uuid;

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }
}
